package android.textIt.frontend.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.textIt.R;
import android.textIt.backend.dao.GroupDao;
import android.textIt.backend.dao.UserDao;
import android.textIt.backend.model.Group;
import android.textIt.backend.model.User;
import android.textIt.frontend.adapter.MemberListAdapter;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class GroupDetailActivity extends Activity {
	private final static int ACTIVITY_GROUPMEMBER = 0;
	int groupId;
  static UserDao userDAO;
  static GroupDao groupDAO;
  Group group;
  List<User> origGroupMembers;
  List<User> newGroupMembers;
  
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.layout_group_detail);   
      userDAO = new UserDao(this);
      groupDAO = new GroupDao(this);
	    Bundle extras = getIntent().getExtras();
	    groupId = Integer.parseInt(extras.getString("id"));
      
      if (groupId > 0){
      	group = groupDAO.getGroupById(groupId);

      	TextView gnView = (TextView)findViewById(R.id.TxtGroupDetailName);
      	gnView.setText(group.getName());
  	    
      	origGroupMembers = groupDAO.getUsersInGroup(group);
        ListView listView = (ListView)findViewById(R.id.listGroupMembers);
        MemberListAdapter madapter = new MemberListAdapter(this, origGroupMembers);
        listView.setAdapter(madapter);
      }
      
      attachButtonOnClickListners();
  }
  
	private void attachButtonOnClickListners() {
		ImageButton btnContacts=(ImageButton)findViewById(R.id.btnContacts);
    btnContacts.setOnClickListener(new Button.OnClickListener() {
          public void onClick(View v) {
        		Intent intent = new Intent();
        		intent.putExtra( "id", groupId); 
          	intent.setAction("Group.Members");
          	startActivityForResult(intent, ACTIVITY_GROUPMEMBER);
          	return; 
          }
      });
    
		Button btnSave=(Button)findViewById(R.id.btnSave);
    btnSave.setOnClickListener(new Button.OnClickListener() {
          public void onClick(View v) {
          	Intent result = new Intent();
          	TextView gnView = (TextView)findViewById(R.id.TxtGroupDetailName);
      			Group g;
      			
      			// Include the phone owner in every group
      			newGroupMembers.add(MainActivity.phoneOwner);
      			
      			if (groupId > 0) { // editing existing group
      				g = groupDAO.getGroupById(groupId);
      				g.setName(gnView.getText().toString());
        			groupDAO.removeUsersFromGroup(g, origGroupMembers); 
        			groupDAO.addUserstoGroup(g, newGroupMembers); 
      				g = groupDAO.getGroupById(groupId);
        			groupDAO.updateGroup(g);
      			}
      			else {
      				g = new Group();
        			g.setIsTemporary(false);
        			g.setName(gnView.getText().toString());
        			long gid = groupDAO.createGroup(g);       				
        			g = groupDAO.getGroupById((int) gid);
        			groupDAO.addUserstoGroup(g, newGroupMembers); 
      			}
      			groupDAO.registerGroup(g);
	        	setResult(RESULT_OK, result);
	        	finish();
          	return; 
          }
      });
    
    Button btnCancel=(Button)findViewById(R.id.btnCancel);
    btnCancel.setOnClickListener(new Button.OnClickListener() {
          public void onClick(View v) {
          	Intent result = new Intent();
	        	setResult(RESULT_CANCELED, result);
	        	finish();
          	return; 
          }
      });	
  }
	
	@Override
  protected void onActivityResult(int requestCode, int resultCode,  Intent extras) {
		switch(resultCode) {
			case Activity.RESULT_OK:
				ArrayList<Integer> memberIdList = extras.getIntegerArrayListExtra("members");
				newGroupMembers = new ArrayList<User>();
        // Get user given the id and add to the list
        for (int id: memberIdList){
        	User u = userDAO.getUserbyID(id);
        	newGroupMembers.add(u);
        }
        // Set the given group's members to the newly created list
        ListView listView = (ListView)findViewById(R.id.listGroupMembers);
        MemberListAdapter madapter = new MemberListAdapter(this, newGroupMembers);
        listView.setAdapter(madapter);
        listView.invalidate();
				break;
	
			case Activity.RESULT_CANCELED:
	    	break;
		}
	}
}
